/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;
import models.Department;
import models.Institute;
import models.StudyProgram;

/**
 *
 * @author Ida
 */
@SessionScoped
@Named(value="studyProgDepInst")
public class A_Depricated_StudyProgDepInst {
    private final ArrayList<StudyProgram> studyprogram = new ArrayList<StudyProgram>();
    private final ArrayList<Department> department = new ArrayList<Department>();
    private final ArrayList<Institute> institute = new ArrayList<Institute>();
    private final DatabaseHjelper dbh = DatabaseHjelper.hent();

    public A_Depricated_StudyProgDepInst(){
        updateStudyProgDepInst();
    }

    private void updateStudyProgDepInst(){
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            conn = dbh.finnForbindelse();
            stmt = conn.prepareStatement("select * from institute");
            res = stmt.executeQuery();
            synchronized (institute) {
                institute.clear();
                while (res.next()){
                    Institute i = new Institute(res.getInt("instituteID"), res.getString("instituteName"), res.getString("instAddress"), res.getString("phoneNr"));
                    //System.out.println(s.toString());
                    institute.add(i);
                }
            }      
            stmt = conn.prepareStatement("select * from department");
            res = stmt.executeQuery();
                synchronized (department) {
                department.clear();
                while (res.next()){
                    Department d = new Department(res.getInt("departmentID"), res.getString("departmentName"), res.getString("depAddress"), res.getString("phoneNr"));
                    //System.out.println(s.toString());
                    department.add(d);
                }
            }
            stmt = conn.prepareStatement("select * from studyProgram");
            res = stmt.executeQuery();
            synchronized (studyprogram) {
                studyprogram.clear();
                while (res.next()){
                    StudyProgram s = new StudyProgram(res.getInt("studyProgramID"), res.getInt("studyProgramCode"), res.getString("studyProgramName"),
                            res.getDate("approved"), res.getDate("lastRevised"), res.getString("approvedBy"));
                    s.setIsNKRFromInt(res.getInt("isNKR"));
                    //System.out.println(s.toString());Department department)
                    studyprogram.add(s);
                }
            }
        }catch (SQLException e) {
            System.out.println(e.getMessage());
        } finally {
            DatabaseHjelper.lukk(res);
            DatabaseHjelper.lukk(stmt);
            DatabaseHjelper.lukk(conn);
        }
    }

    public List<Institute> getInstitute(){
        return institute;
    }

    public List<StudyProgram> getStudyprogram(){
        return studyprogram;
    }

    public List<Department> getDepartment(){
        return department;
    }


}
